package com.zhenyi.jiahua.api;

import java.util.ArrayList;
import java.util.List;

import com.cenjiawen.common.IndexController;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbPro;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.zhenyi.jiahua.interceptor.ApiLogInterceptor;

import cn.hutool.core.date.DateUtil;

@Clear
@Before(ApiLogInterceptor.class)
public class ApiController extends IndexController {

	/**
	 * {"className":"core","Time:13:00","teacherName":"Joey","studentList":[
	 * "John", "Lee","larry"]}
	 * 
	 * @Time 2018年12月11日 下午5:59:50
	 */
	public void classes() {
		Kv todayKc = Kv.create();
		todayKc.set("startTime", DateUtil.today() + " 00:00:00");
		todayKc.set("endTime", DateUtil.today() + " 23:59:59");
		DbPro db = Db.use("jiahua");
		SqlPara sqlPara = db.getSqlPara("JHApiCourseList", todayKc);
		List<Record> items = db.find(sqlPara);
		for (Record record : items) {
			Long arrangeId = record.getLong("id");
			record.remove("id");
			SqlPara stuPara = db.getSqlPara("JHApiCourseStudents", Kv.by("arrangeId", arrangeId));
			List<Record> students = db.find(stuPara);
			ArrayList<String> studentNames = new ArrayList<>();
			for (Record student : students) {
				studentNames.add(student.getStr("name"));
			}
			record.set("studentList", studentNames);
		}
		renderJson(items);
	}
}
